package yedean.bean;

import com.yedean.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * ProxyUserDao
 *
 * @author yedean
 * @date 2022/7/14
 */
public class ProxyUserDao implements FactoryBean<IUserDao> {
    @Override
    public IUserDao getObject() {
        InvocationHandler handler = (proxy, method, args) -> {
            // 添加排除方法
            if ("toString".equals(method.getName())) return this.toString();
            
            Map<String,String> map=new HashMap<>();
            map.put("10001", "小傅哥");
            map.put("10002", "八杯水");
            map.put("10003", "阿毛");
            
            return "你被代理了"+method.getName()+":"+map.get(args[0].toString());
        };

        return (IUserDao) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{IUserDao.class},handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
